package com.irdstudio.bsp.executor.core.plugin.datajob;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据表操作类-[表名: 02.数据作业参数配置(plugin_job_param)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-04-24
 */
public class PluginJobParamDao {

	private static final Logger logger = LoggerFactory.getLogger(PluginJobParamDao.class);
	/* 连接对象 */
	Connection conn = null;	

	public PluginJobParamDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public PluginJobParam queryWithKeys(String paramGroupId,String jobParamName)
			throws SQLException {
		PluginJobParam dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_job_param WHERE param_group_id=?,job_param_name=?");
			ps.setString(1,paramGroupId);
			ps.setString(2,jobParamName);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new PluginJobParam();
				dc.setParamGroupId(rs.getString("param_group_id"));
				dc.setJobParamName(rs.getString("job_param_name"));
				dc.setJobParamValue(rs.getString("job_param_value"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryPluginJobParamWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}
	
	public List<PluginJobParam> queryWithParamGroupId(String dsParams)
			throws SQLException {
		List<PluginJobParam> dcList = new ArrayList<PluginJobParam>();
		PluginJobParam dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM plugin_job_param WHERE ds_params=?");
			ps.setString(1,dsParams);
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new PluginJobParam();
				dc.setParamGroupId(rs.getString("param_group_id"));
				dc.setJobParamName(rs.getString("job_param_name"));
				dc.setJobParamValue(rs.getString("job_param_value"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryWithParamGroupId is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
//	public List<PluginJobParam> queryWithCond(String szCondition,String szOrderBy)
//			throws SQLException {
//		List<PluginJobParam> dcList = new ArrayList<PluginJobParam>();
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		try {
//			ps = conn.prepareStatement("SELECT * FROM plugin_job_param "
//					+ szCondition + " " + szOrderBy);
//			rs = ps.executeQuery();
//			while (rs.next()) {
//				PluginJobParam dc = new PluginJobParam();
//				dc.setParamGroupId(rs.getString("param_group_id"));
//				dc.setJobParamName(rs.getString("job_param_name"));
//				dc.setJobParamValue(rs.getString("job_param_value"));
//				dcList.add(dc);
//			}
//		} catch (SQLException e) {
//			throw new SQLException("queryPluginJobParamWithCond is Wrong!"
//					+ e.getMessage());
//		} finally {
//			close(rs, null, ps);
//		}
//		return dcList;
//	}

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}
	}

}
